package com.duofan.gmall.product.service.impl;

import com.duofan.gmall.product.dao.CategoryDao;
import com.duofan.gmall.product.entity.AttrEntity;
import com.duofan.gmall.product.entity.CategoryEntity;
import com.duofan.gmall.product.service.AttrService;
import com.duofan.gmall.product.vo.AttrGroupWithAttrsVo;
import com.duofan.gmall.product.vo.SkuItemVo;
import com.duofan.gmall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.duofan.gmall.common.utils.PageUtils;
import com.duofan.gmall.common.utils.Query;

import com.duofan.gmall.product.dao.AttrGroupDao;
import com.duofan.gmall.product.entity.AttrGroupEntity;
import com.duofan.gmall.product.service.AttrGroupService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    CategoryDao categoryDao;
    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String)params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if(catelogId != 0) {
            wrapper.eq("catelog_id", catelogId);
        }
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                wrapper);
        page.setRecords(setCatelogNames(page.getRecords()));
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsVo> groupWithAttrsVos = groupEntities.stream().map(groupEntity -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(groupEntity, attrsVo);
            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getAttrGroupId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());
        return groupWithAttrsVos;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        // 查当前spu对应的所有属性的分组信息及当前分组下所有属性对应的值
        // 1.当前spu有多少对应的属性分组
        List<SpuItemAttrGroupVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
        return vos;
    }

    /**
     * 关联分类表中的名称
     * @param entities
     * @return
     */
    private List<AttrGroupEntity> setCatelogNames(List<AttrGroupEntity> entities) {
        List<Long> catelogIds = entities.stream().map(e -> e.getCatelogId()).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(catelogIds)) {
            List<CategoryEntity> categoryEntities = categoryDao.selectList(new QueryWrapper<CategoryEntity>().in("cat_id", catelogIds));
            Map<Long, String> map = new HashMap();
            categoryEntities.forEach(e -> {
                map.put(e.getCatId(), e.getName());
            });
            entities.forEach(e -> {
                e.setCatelogName(map.get(e.getCatelogId()));
            });
            return entities;
        }
        return entities;
    }

}